<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
</head>

<body>
  <script>

    $.ajax({

      type: 'POST',

      url: 'http://api.jiaoshang.cc/common/api/ip2Localtion',

      data: {},

      success: function (res) {
        loginFormH5(res.result.ad_info)
      },

      dataType: 'json'

    });
    function loginFormH5(data) {
      function guid2() {
        function S4() {
          return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
      }

      let wsData = {
        uid: guid2(),
        location: data.location,
        province: data.province,
        city: escape(data.city).replace(/\%u/g, '\\u'),
        adcode: data.adcode,
        city_code: data.city_code
      }
      console.log(wsData)

      // 连接服务端，workerman.net:2120换成实际部署web-msg-sender服务的域名或者ip
      var socket = io('http://socket.wxtsbf.com:2120');

      // socket连接后以uid登录
      socket.on('connect', function () {
        socket.emit('loginFormH5', wsData);
      });
      // 后端推送来消息时
      socket.on('new_msg', function (msg) {
        console.log("收到消息：" + msg);
      });
      socket.on('loginSuccessFormH5', function (msg) {
        console.log("登陆消息：" + msg);
      });
      // 后端推送来在线数据时
      socket.on('update_online_count', function (online_stat) {
        console.log(online_stat);
      });
    }
  </script>
</body>

</html>